home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / C++ Spline Class V 1.02 / C++ Spline Class 1.02.sea / Spline Class / 2DSpline.test.cp < prev    next >
Text File  |  1993-10-01  |  20KB  |  603 lines

  1. /************************************************************************************************/
  2. /*                                                         Chris Marshall                                                    */
  3. /*                                                                                                                                */
  4. /*                                                Nikon Electronic Imaging Dept                                            */
  5. /*                                                    1300 Walt Whitman Road                                                */
  6. /*                                                      Melville, NY 11747                                                    */
  7. /*                                                         (516) 547-4200                                                    */
  8. /*                                                                                                                                */
  9. /************************************************************************************************/
  10. /*        File Name:            2DSpline.test.cp                                                                            */
  11. /*        Description:        Test application file for a 2-dimensional B-spline class                        */
  12. /*        Primary Author:    Chris Marshall                                                                                */
  13. /*        Version:                1.02                                                                                            */
  14. /*        Project:                Library file                                                                                */
  15. /*        Compiler(s):        Symantec C++ 6.0                                                                            */
  16. /************************************************************************************************/
  17. /*                                                     MODIFICATION HISTORY                                                */
  18. /************************************************************************************************/
  19. /*    Date            Author                Description                                                                        */
  20. /*                                                                                                                                */
  21. /*    10/01/93        Chris Marshall        I added a little snippet of code to copy the spline points        */
  22. /*                                            when switching from fixed point to floating point modes.            */
  23. /*                                                                                                                                */
  24. /************************************************************************************************/
  25.  
  26. #include    "2DSpline.h"
  27.  
  28. #define    MaxVal(a,b)    ((a > b) ? a : b)
  29. #define    MinVal(a,b)    ((a < b) ? a : b)
  30.  
  31. Spline    *gMySpline;
  32. long        *gOldLine;
  33. Point        *gOldPoints;
  34. short        gOldSelectedPoint;
  35. Boolean    gLineDrawn;
  36.  
  37. #define    kDrawSelectedPoint        0
  38. #define    kRedrawPoint                1
  39. #define    kDrawNew                        2
  40.  
  41. #define    kLeftArrowKey                0x1C
  42. #define    kRightArrowKey                0x1D
  43. #define    kUpArrowKey                    0x1E
  44. #define    kDownArrowKey                0x1F
  45.  
  46. #define    kTabKey                        0x09
  47. #define    kDeleteKey                    0x08
  48.  
  49. #define    kHitRadius                     4
  50.  
  51. void DisplayWindowCentered ( GrafPtr theWindow );
  52. void CenterWindow ( GrafPtr theWindow );
  53. void DrawControlPoint ( Rect box, short x, short y, Boolean selected );
  54. void DrawSplineLine ( Rect box, short selector );
  55. pascal Boolean FilterProc ( DialogPtr theDialog, EventRecord *theEvent, short *theItem );
  56.  
  57. /************************************************************************************************/
  58. /*        Function Name:        CenterWindow                                                                                */
  59. /*        Description:        Centers a window on the screen                                                        */
  60. /*        Primary Author:    Chris Marshall                                                                                */
  61. /************************************************************************************************/
  62. /*                                                    MODIFICATION HISTORY                                                    */
  63. /************************************************************************************************/
  64. /*    Date            Author                Description                                                                        */
  65. /*                                                                                                                                */
  66. /************************************************************************************************/
  67. /*##############################################################################################*/
  68. /************************************************************************************************/
  69.  
  70. void CenterWindow ( GrafPtr theWindow )
  71. {
  72.     short        wHeight, wWidth, dHeight, dWidth;
  73.     GrafPtr    wmPort;
  74.     
  75.     if ( theWindow )                    /* Make sure that the window is kosher    */
  76.         {
  77.         /* Calculate the window's height and width    */
  78.         wHeight = theWindow->portRect.bottom - theWindow->portRect.top;
  79.         wWidth = theWindow->portRect.right - theWindow->portRect.left;
  80.         
  81.         GetWMgrPort ( &wmPort );    /* Get the primary display window    */
  82.         
  83.         /* Calculate it's height and width    */
  84.         dHeight = wmPort->portRect.bottom - (wmPort->portRect.top + 29);
  85.         dWidth = wmPort->portRect.right - wmPort->portRect.left;
  86.         
  87.         /* Center the window in that window    */
  88.         MoveWindow ( theWindow, ((dWidth - wWidth) / 2) - theWindow->portRect.left,
  89.                         (((dHeight - wHeight) / 3) - theWindow->portRect.top) + 29, false );
  90.         }
  91. }
  92.  
  93. /************************************************************************************************/
  94. /*        Function Name:        DisplayWindowCentered                                                                    */
  95. /*        Description:        Displays a window centered on the screen                                            */
  96. /*        Primary Author:    Chris Marshall                                                                                */
  97. /************************************************************************************************/
  98. /*                                                    MODIFICATION HISTORY                                                    */
  99. /************************************************************************************************/
  100. /*    Date            Author                Description                                                                        */
  101. /*                                                                                                                                */
  102. /************************************************************************************************/
  103. /*##############################################################################################*/
  104. /************************************************************************************************/
  105. /* This routine takes a window pointer, and then centers the window on the screen.                    */
  106. /************************************************************************************************/
  107.  
  108. void DisplayWindowCentered ( GrafPtr theWindow )
  109. {
  110.     if ( theWindow )                    /* Make sure that the window is kosher    */
  111.         {
  112.         CenterWindow ( theWindow );
  113.         ShowWindow ( theWindow );            /* Show the window    */
  114.         SelectWindow ( theWindow );
  115.         SetPort ( theWindow );
  116.         }
  117. }
  118.  
  119. /************************************************************************************************/
  120. /*        Function Name:        DrawControlPoint                                                                            */
  121. /*        Description:        Draws our little dots                                                                    */
  122. /*        Primary Author:    Chris Marshall                                                                                */
  123. /************************************************************************************************/
  124. /*                                                    MODIFICATION HISTORY                                                    */
  125. /************************************************************************************************/
  126. /*    Date            Author                Description                                                                        */
  127. /*                                                                                                                                */
  128. /************************************************************************************************/
  129. /*##############################################################################################*/
  130. /************************************************************************************************/
  131.  
  132. void DrawControlPoint ( Rect box, short x, short y, Boolean selected )
  133. {
  134.     Rect        pointRect;
  135.     PenState    oldPen;
  136.     
  137.     GetPenState ( &oldPen );
  138.     PenNormal ( );
  139.     PenMode ( patXor );
  140.     
  141.     if ( selected )
  142.         {
  143.         SetRect ( &pointRect, 0, 0, 4, 4 );
  144.         PenSize ( 2, 2 );
  145.         OffsetRect ( &pointRect, box.left + (box.left + x) - 2, (box.bottom - y) - 2 );
  146.         FrameOval ( &pointRect );
  147.         PenSize ( 1, 1 );
  148.         SetRect ( &pointRect, 0, 0, 8, 8 );
  149.         OffsetRect ( &pointRect, box.left + (box.left + x) - 4, (box.bottom - y) - 4 );
  150.         }
  151.     else
  152.         {
  153.         SetRect ( &pointRect, 0, 0, 4, 4 );
  154.         PenSize ( 2, 2 );
  155.         OffsetRect ( &pointRect, box.left + (box.left + x) - 2, (box.bottom - y) - 2 );
  156.         }
  157.     
  158.     FrameOval ( &pointRect );
  159.     SetPenState ( &oldPen );
  160. }
  161.  
  162. /************************************************************************************************/
  163. /*        Function Name:        DrawSplineLine                                                                                */
  164. /*        Description:        Handles drawing of the spline line                                                    */
  165. /*        Primary Author:    Chris Marshall                                                                                */
  166. /************************************************************************************************/
  167. /*                                                    MODIFICATION HISTORY                                                    */
  168. /************************************************************************************************/
  169. /*    Date            Author                Description                                                                        */
  170. /*                                                                                                                                */
  171. /************************************************************************************************/
  172. /*##############################################################################################*/
  173. /************************************************************************************************/
  174.  
  175. void DrawSplineLine ( Rect box, short selector )
  176. {
  177.     RgnHandle        oldClip;
  178.     register short    maxX = box.right - box.left, index;
  179.     long                x, y;
  180.     PenState            oldPen;
  181.     Pattern            pat;
  182.     short                point;
  183.     
  184.     oldClip = NewRgn ( );
  185.     
  186.     if ( oldClip )
  187.         {
  188.         RGBColor    oldFore, oldBack;
  189.         Rect        box2;
  190.         
  191.         GetClip ( oldClip );
  192.         GetPenState ( &oldPen );
  193.         PenNormal ( );
  194.         
  195.         GetForeColor ( &oldFore );
  196.         GetBackColor ( &oldBack );
  197.         
  198.         ForeColor ( blackColor );
  199.         BackColor ( whiteColor );
  200.         
  201.         ClipRect ( &box );
  202.         
  203.         if ( selector == kDrawNew )
  204.             EraseRect ( &box );
  205.         
  206.         PenMode ( patXor );
  207.         
  208.         switch ( selector )
  209.             {
  210.             case    kDrawSelectedPoint:
  211.                 point = gMySpline->GetSelectedPoint ( );
  212.                 
  213.                 if ( point )
  214.                     {
  215.                     if ( (point != gOldSelectedPoint) && gOldSelectedPoint )
  216.                         {
  217.                         x = gOldPoints[gOldSelectedPoint - 1].h;
  218.                         y = gOldPoints[gOldSelectedPoint - 1].v;
  219.                     
  220.                         DrawControlPoint ( box, x, y, true );
  221.                         DrawControlPoint ( box, x, y, false );
  222.                         }
  223.                     
  224.                     x = gOldPoints[--point].h;
  225.                     y = gOldPoints[point].v;
  226.                     DrawControlPoint ( box, x, y, point == (gOldSelectedPoint - 1) );
  227.                     DrawControlPoint ( box, x, y, true );
  228.                     gOldSelectedPoint = point + 1;
  229.                     }
  230.             break;
  231.             
  232.             case    kRedrawPoint:
  233.                 x = gOldPoints[gOldSelectedPoint - 1].h;
  234.                 y = gOldPoints[gOldSelectedPoint - 1].v;
  235.                 
  236.                 DrawControlPoint ( box, x, y, true );
  237.                 
  238.                 if ( gOldSelectedPoint != gMySpline->GetSelectedPoint ( ) )
  239.                     {
  240.                     index = gMySpline->GetNumberOfSplinePoints ( );
  241.                     
  242.                     while ( index )
  243.                         {
  244.                         gMySpline->GetSplinePoint ( index--, x, y );
  245.                         gOldPoints[index].h = x;
  246.                         gOldPoints[index].v = y;
  247.                         }
  248.                     }
  249.                 
  250.                 gOldSelectedPoint = gMySpline->GetSplinePoint ( x, y );
  251.                 gOldPoints[gOldSelectedPoint - 1].h = x;
  252.                 gOldPoints[gOldSelectedPoint - 1].v = y;
  253.                 DrawControlPoint ( box, x, y, true );
  254.                 
  255.                 for ( x = 0; x < maxX; x++ )
  256.                     {
  257.                     MoveTo ( box.left + x, box.bottom - gOldLine[x] );
  258.                     Line ( 0, 0 );
  259.                     
  260.                     gOldLine[x] = y = gMySpline->GetYValue ( x );
  261.                     MoveTo ( box.left + x, box.bottom - y );
  262.                     Line ( 0, 0 );
  263.                     }
  264.             break;
  265.         
  266.             case    kDrawNew:
  267.                 EraseRect ( &box );
  268.  
  269.                 gOldSelectedPoint = gMySpline->GetSelectedPoint ( );
  270.                 
  271.                 index = gMySpline->GetNumberOfSplinePoints ( );
  272.                 
  273.                 while ( index )
  274.                     {
  275.                     gMySpline->GetSplinePoint ( index--, x, y );
  276.                     gOldPoints[index].h = x;
  277.                     gOldPoints[index].v = y;
  278.                     
  279.                     DrawControlPoint ( box, x, y, index == (gOldSelectedPoint - 1) );
  280.                     }
  281.                 
  282.                 PenSize ( 1, 1 );
  283.                 
  284.                 for ( x = 0; x < maxX; x++ )
  285.                     {
  286.                     gOldLine[x] = y = gMySpline->GetYValue ( x );
  287.                     MoveTo ( box.left + x, box.bottom - y );
  288.                     Line ( 0, 0 );
  289.                     }
  290.             break;
  291.             }
  292.         
  293.         RGBForeColor ( &oldFore );
  294.         RGBBackColor ( &oldBack );
  295.         
  296.         SetPenState ( &oldPen );
  297.         SetClip ( oldClip );
  298.         DisposeRgn ( oldClip );
  299.         }
  300. }
  301.  
  302. /************************************************************************************************/
  303. /*        Function Name:        FilterProc                                                                                    */
  304. /*        Description:        Hacky filter proc - don't do this at home, kids!                                */
  305. /*        Primary Author:    Chris Marshall                                                                                */
  306. /************************************************************************************************/
  307. /*                                                    MODIFICATION HISTORY                                                    */
  308. /************************************************************************************************/
  309. /*    Date            Author                Description                                                                        */
  310. /*                                                                                                                                */
  311. /************************************************************************************************/
  312. /*##############################################################################################*/
  313. /************************************************************************************************/
  314.  
  315. pascal Boolean FilterProc ( DialogPtr theDialog, EventRecord *theEvent, short *theItem )
  316. {
  317.     Point    where = theEvent->where;
  318.     short                type;
  319.     Handle            item;
  320.     Rect                box;
  321.     short                numPoints = gMySpline->GetNumberOfSplinePoints ( );
  322.     long                x, y;
  323.     
  324.     GlobalToLocal ( &where );
  325.     
  326.     GetDItem ( theDialog, 2, &type, &item, &box );
  327.     
  328.     if ( (theEvent->what == keyDown) || (theEvent->what == autoKey) )
  329.         switch ( theEvent->message & charCodeMask )
  330.             {
  331.             case    kDeleteKey:    // The user can delete spline points
  332.                 if ( numPoints > 2 )
  333.                     {
  334.                     gMySpline->DeleteSplinePoint ( );
  335.                     DrawSplineLine ( box, kDrawNew );
  336.                     }
  337.                 else
  338.                     SysBeep ( 1 );
  339.                 
  340.                 *theItem = 2;
  341.                 return true;
  342.             break;
  343.         
  344.             case    kTabKey:    // The user can tab from point to point
  345.                 short    selectedPoint = gMySpline->GetSelectedPoint ( );
  346.                 
  347.                 if ( theEvent->modifiers & shiftKey )
  348.                     {
  349.                     if ( --selectedPoint < 1 )
  350.                         selectedPoint = numPoints;
  351.                     }
  352.                 else
  353.                     if ( ++selectedPoint > numPoints )
  354.                         selectedPoint = 1;
  355.                 
  356.                 gMySpline->SelectPoint ( selectedPoint );
  357.                 DrawSplineLine ( box, kDrawSelectedPoint );
  358.                 
  359.                 *theItem = 2;
  360.                 return true;
  361.             break;
  362.             
  363.             case    kLeftArrowKey:
  364.             case    kRightArrowKey:
  365.             case    kUpArrowKey:
  366.             case    kDownArrowKey:
  367.                 gMySpline->GetSplinePoint ( x, y );
  368.                 
  369.                 switch ( theEvent->message & charCodeMask )
  370.                     {
  371.                     case    kLeftArrowKey:
  372.                         if ( theEvent->modifiers & shiftKey )
  373.                             x -= 10;
  374.                         else
  375.                             x--;
  376.                     break;
  377.                     
  378.                     case    kRightArrowKey:
  379.                         if ( theEvent->modifiers & shiftKey )
  380.                             x += 10;
  381.                         else
  382.                             x++;
  383.                     break;
  384.                     
  385.                     case    kUpArrowKey:
  386.                         if ( theEvent->modifiers & shiftKey )
  387.                             y += 10;
  388.                         else
  389.                             y++;
  390.                     break;
  391.                     
  392.                     case    kDownArrowKey:
  393.                         if ( theEvent->modifiers & shiftKey )
  394.                             y -= 10;
  395.                         else
  396.                             y--;
  397.                     break;
  398.                     }
  399.                 
  400.                 x = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, x ) );
  401.                 y = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, y ) );
  402.     
  403.                 gMySpline->ChangeSplinePoint ( x, y );
  404.                 DrawSplineLine ( box, kRedrawPoint );
  405.     
  406.                 *theItem = 2;
  407.                 return true;
  408.             break;
  409.             }
  410.     
  411.     if ( (theEvent->what == mouseDown) && (FindDItem ( theDialog, where ) == 1) )
  412.         {
  413.         Point                oldWhere;
  414.         short                index;
  415.         
  416.         where.h -= box.left;
  417.         where.v -= box.top;
  418.  
  419.         where.h = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, where.h ) );
  420.         where.v = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, where.v ) );
  421.  
  422.         oldWhere = where;
  423.         
  424.         y = ((box.bottom - box.top) - 1) - where.v;
  425.         x = where.h;
  426.         
  427.         index = gMySpline->IsASplinePoint ( x, y );
  428.         
  429.         if ( index )
  430.             {
  431.             gMySpline->SelectPoint ( index );
  432.             DrawSplineLine ( box, kDrawSelectedPoint );
  433.             }
  434.         else
  435.             {
  436.             index = gMySpline->AddSplinePoint ( where.h, ((box.bottom - box.top) - 1) - where.v );
  437.             DrawSplineLine ( box, kDrawNew );
  438.             }
  439.         
  440.         while ( StillDown ( ) )
  441.             {
  442.             GetDItem ( theDialog, 2, &type, &item, &box );
  443.             GetMouse ( &where );
  444.             where.h -= box.left;
  445.             where.v -= box.top;
  446.             
  447.             if ( (where.h < -20) || (where.h > (box.right - box.left) + 20) )
  448.                 {
  449.                 if ( (index > 1) && (index < gMySpline->GetNumberOfSplinePoints ( )) )
  450.                     {
  451.                     gMySpline->DeleteSplinePoint ( );
  452.                     DrawSplineLine ( box, kDrawNew );
  453.                     break;
  454.                     }
  455.                 }
  456.  
  457.             where.h = MinVal ( (box.right - box.left) - 1, MaxVal ( 0, where.h ) );
  458.             where.v = MinVal ( (box.bottom - box.top) - 1, MaxVal ( 0, where.v ) );
  459.             
  460.             if ( (where.h != oldWhere.h) || (where.v != oldWhere.v) )
  461.                 {
  462.                 y = ((box.bottom - box.top) - 1) - where.v;
  463.                 x = where.h;
  464.                 
  465.                 index = gMySpline->ChangeSplinePoint ( x, y );
  466.                 
  467.                 DrawSplineLine ( box, kRedrawPoint );
  468.                 
  469.                 oldWhere = where;
  470.                 }
  471.             }
  472.  
  473.         *theItem = 2;
  474.         return true;
  475.         }
  476.     else
  477.         return false;
  478. }
  479.  
  480. /************************************************************************************************/
  481. /*        Function Name:        main                                                                                            */
  482. /*        Description:        Main context of the test app.                                                            */
  483. /*        Primary Author:    Chris Marshall                                                                                */
  484. /************************************************************************************************/
  485. /*                                                    MODIFICATION HISTORY                                                    */
  486. /************************************************************************************************/
  487. /*    Date            Author                Description                                                                        */
  488. /*                                                                                                                                */
  489. /*    10/01/93        Chris Marshall        I added a little snippet of code to copy the spline points        */
  490. /*                                            when switching from fixed point to floating point modes.            */
  491. /*                                                                                                                                */
  492. /************************************************************************************************/
  493. /*##############################################################################################*/
  494. /************************************************************************************************/
  495.  
  496. void main ( )
  497. {
  498.     DialogPtr    theDialog;
  499.     short            type;
  500.     Handle        item;
  501.     Rect            box, box2;
  502.     short            theItem, numPoints;
  503.     
  504.     InitGraf ( &thePort );
  505.     InitFonts ( );
  506.     InitWindows ( );
  507.     InitMenus ( );
  508.     TEInit ( );
  509.     InitCursor ( );
  510.     InitDialogs (0L);
  511.     FlushEvents ( everyEvent, 0 );
  512.     MaxApplZone ( );
  513.     
  514.     MoreMasters ( );
  515.     MoreMasters ( );
  516.     MoreMasters ( );
  517.     
  518.     theDialog = GetNewDialog ( 128, 0L, 0L );
  519.  
  520.     if ( theDialog )
  521.         {
  522.         GetDItem ( theDialog, 3, &type, &item, &box );
  523.         SetCtlValue ( (ControlHandle)item, 1 );
  524.         
  525.         GetDItem ( theDialog, 2, &type, &item, &box );
  526.         gMySpline = (Spline*)new FixedSpline ( 0, 0, (box.right - box.left) - 1,
  527.                                                                     (box.bottom - box.top) - 1, kHitRadius );
  528.         
  529.         if ( gMySpline )
  530.             {
  531.             // We just give enough points for the whole line and say the heck with it
  532.             gOldLine = (long*)NewPtr ( sizeof ( long ) * (box.right - box.left) );
  533.             gOldPoints = (Point*)NewPtr ( sizeof ( Point ) * (box.right - box.left) );
  534.             
  535.             SetPort ( theDialog );
  536.     
  537.             DisplayWindowCentered ( theDialog );
  538.             
  539.             BackColor ( whiteColor );
  540.             ForeColor ( blackColor );
  541.             EraseRect ( &box );
  542.             InsetRect ( &box, -1, -1 );
  543.             FrameRect ( &box );
  544.             InsetRect ( &box, 1, 1 );
  545.             
  546.             DrawSplineLine ( box, kDrawNew );
  547.             
  548.             do
  549.                 {
  550.                 ModalDialog ( FilterProc, &theItem );
  551.                 numPoints = gMySpline->GetNumberOfSplinePoints ( );
  552.                 switch ( theItem )    // Let the user switch classes
  553.                     {
  554.                     case    3:
  555.                         GetDItem ( theDialog, 3, &type, &item, &box2 );
  556.                         
  557.                         if ( !GetCtlValue ( (ControlHandle)item ) )
  558.                             {
  559.                             SetCtlValue ( (ControlHandle)item, 1 );
  560.                             GetDItem ( theDialog, 4, &type, &item, &box2 );
  561.                             SetCtlValue ( (ControlHandle)item, 0 );
  562.                             delete gMySpline;
  563.                             gMySpline = (Spline*)new FixedSpline ( gOldPoints[0].h, gOldPoints[0].v,
  564.                                                     gOldPoints[--numPoints].h,
  565.                                                     gOldPoints[numPoints].v, kHitRadius );
  566.                             while ( numPoints > 1 )
  567.                                 gMySpline->AddSplinePoint ( gOldPoints[--numPoints].h,
  568.                                                                         gOldPoints[numPoints].v );
  569.                             DrawSplineLine ( box, kDrawNew );
  570.                             }
  571.                     break;
  572.                     
  573.                     case    4:
  574.                         GetDItem ( theDialog, 4, &type, &item, &box2 );
  575.                         
  576.                         if ( !GetCtlValue ( (ControlHandle)item ) )
  577.                             {
  578.                             SetCtlValue ( (ControlHandle)item, 1 );
  579.                             GetDItem ( theDialog, 3, &type, &item, &box2 );
  580.                             SetCtlValue ( (ControlHandle)item, 0 );
  581.                             delete gMySpline;
  582.                             gMySpline = (Spline*)new Spline ( gOldPoints[0].h, gOldPoints[0].v,
  583.                                                     gOldPoints[--numPoints].h,
  584.                                                     gOldPoints[numPoints].v, kHitRadius );
  585.                             while ( numPoints > 1 )
  586.                                 gMySpline->AddSplinePoint ( gOldPoints[--numPoints].h,
  587.                                                                         gOldPoints[numPoints].v );
  588.                             DrawSplineLine ( box, kDrawNew );
  589.                             }
  590.                     break;
  591.                     }
  592.                 } while ( theItem != 1 );
  593.             
  594.             DisposPtr ( (Ptr)gOldLine );
  595.             DisposPtr ( (Ptr)gOldPoints );
  596.             
  597.             delete gMySpline;
  598.             }
  599.         
  600.         DisposDialog ( theDialog );
  601.         }
  602. }
  603.